﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEL.TelPhoneF.Controls
{
    public partial class VolueBar : UserControl
    {
        public VolueBar()
        {
            InitializeComponent();
            m_yBar = this.Size.Height/2;
            m_MinPoint = new Point(this.Location.X+5, m_yBar);
            m_MaxPoint = new Point(this.Size.Width - 5, m_yBar);
        }

        private Bitmap m_image;
        private Bitmap map()
        {
            Bitmap res = new Bitmap(Size.Width, Size.Height);
            Graphics g = Graphics.FromImage(res);
            g.DrawLine(new Pen(m_ColorBackBar), m_MinPoint, m_MaxPoint);
            if (m_ValuePoint != Point.Empty)
            {
                g.DrawLine(new Pen(m_ColorValueBar), m_MinPoint, m_ValuePoint);
            }
            return res;
        }

        private bool m_MouseDown = false;
        private int m_yBar = 12;
        private Point m_MinPoint = Point.Empty;
        private Point m_MaxPoint = Point.Empty;
        private Point m_ValuePoint = Point.Empty;

        private Color m_ColorBackBar = Color.Transparent;
        private Color m_ColorValueBar = Form1.BlueSkinColor;

        public Color ColorValueBar
        {
            get { return m_ColorValueBar; }
            set { m_ColorValueBar = value; }
        }

        private int m_MinValue = 0;
        private int m_MaxValue = 100;
        private int m_Value = 50;

        public event EventHandler ValueChanged;
        private void OnValueChanged()
        {
            int x = (int) (m_Value*K);
            m_ValuePoint = new Point(x + 5,m_yBar);
            m_image = map();
            this.Invalidate();
            if (ValueChanged != null) ValueChanged(this, EventArgs.Empty);
        }
        public int Value
        {
            get { return m_Value; }
            set
            {
                if (m_Value != value)
                {
                    m_Value = value;
                    OnValueChanged();
                }
            }
        }
        private double K = 0;

        private void SetK()
        {
            int b = (m_MaxPoint.X -5) - (m_MinPoint.X-5);
            int v = m_MaxValue - m_MinValue;
            if (v>0) K = (b*1.0)/(v*1.0);

        }

        private void VolueBar_Paint(object sender, PaintEventArgs e)
        {
           if (m_image != null)
                e.Graphics.DrawImage(m_image, 0, 0);
        }

        private void SetValuePoint(int x)
        {
            if (x < m_MinPoint.X) 
                m_ValuePoint= new Point(m_MinPoint.X, m_yBar);
            else if (x> m_MaxPoint.X)
                m_ValuePoint = new Point(m_MaxPoint.X, m_yBar);
            else m_ValuePoint = new Point(x, m_yBar);
            if (m_ValuePoint != Point.Empty) Value = (int) ((m_ValuePoint.X - 5)/K);
            this.Invalidate();
        }

        private void VolueBar_MouseDown(object sender, MouseEventArgs e)
        {
            m_MouseDown = true;
            SetValuePoint(e.X);
        }

        private void VolueBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_MouseDown) SetValuePoint(e.X);
        }

        private void VolueBar_MouseUp(object sender, MouseEventArgs e)
        {
            m_MouseDown = false;
        }

        private void VolueBar_SizeChanged(object sender, EventArgs e)
        {
            m_yBar = this.Size.Height / 2;
            m_MinPoint = new Point(5, m_yBar);
            m_MaxPoint = new Point(this.Size.Width - 5, m_yBar);
            SetK();
        }
    }
}
